<?php

namespace App\Admin\Controllers;

use App\Actions\Grid\TotalRows;
use App\Imports\CertifImports;
use App\Imports\ExhibitorImports;
use App\Models\Certificate;
use App\Models\ExhibitorInformation;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\CsvExporter;
use Encore\Admin\Show;

class CertificateController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '展商证件申请信息';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Certificate());
        $grid->tools(function (Grid\Tools $tools) use ($grid) {
            $tools->append(new CertifImports());
        });
        $grid->filter(function ($filter) {
            // 如果你想要在禁用筛选的情况下保留搜索框，可以添加如下代码
            $filter->disableIdFilter();
            $filter->like('exhibitor_information.name', trans('admin.keywords'));

        });
        $grid->column('name', __('certificates.name'));
        $grid->column('exhibitor_information.name', __('exhibitor_information.name'));
        $grid->column('exhibitor_information.en_name_full', __('exhibitor_information.en_name_full'));
        $grid->column('exhibitor_information.booth_number', __('exhibitor_information.booth_number'));
        $grid->column('phone', __('certificates.phone'));
        $grid->column('e_mail', __('certificates.e_mail'));
        $grid->column('code', __('certificates.code'));
        $grid->column('type_id', __('certificates.type_id'))->using(__('certificates.type_array'));
        $grid->export(function (CsvExporter $export) use ($grid) {
            $export->filename('展商证件申请信息');

            $export->column('code', function ($value, $original) {

                return $value . "\t";//导出字符串格式

            });
            $export->column('exhibitor_information.en_name_full', function ($value, $original) {
                return str_replace('amp;', '', $value);

            });


        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Certificate::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('certificates.name'));
        $show->field('phone', __('certificates.phone'));
        $show->field('e_mail', __('certificates.e_mail'));
        $show->field('code', __('certificates.code'));
        $show->field('type_id', __('certificates.type_id'))->using(__('certificates.type_array'));
        $show->field('exhibitor_information.name', __('certificates.exhibitor_information_id'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Certificate());
        $exhibitor_information = ExhibitorInformation::query()->pluck('name', 'id')->toArray();
        $form->select('exhibitor_information_id', __('certificates.exhibitor_information_id'))
            ->options($exhibitor_information)->rules(['required']);
        $form->text('name', __('certificates.name'));
        $form->text('phone', __('certificates.phone'));
        $form->text('e_mail', __('certificates.e_mail'));
        $form->text('code', __('certificates.code'));
        $form->select('type_id', __('certificates.type_id'))
            ->options(__('certificates.type_array'))->rules(['required']);

        return $form;
    }
}
